package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.produce.domain.req.MachiningReq;
import com.ruibang.glass.produce.domain.resp.MiddleMeasureGraphResp;
import com.ruibang.glass.produce.entity.DbPositionSurvey;
import com.ruibang.glass.produce.entity.DbWheelGroove;
import com.ruibang.glass.produce.entity.MiddleMeasure;
import com.ruibang.glass.produce.mapper.MiddleMeasureMapper;
import com.ruibang.glass.produce.service.MiddleMeasureService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.ZoneOffset;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-01-23  14:31
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Service
public class MiddleMeasureServiceImpl extends ServiceImpl<MiddleMeasureMapper, MiddleMeasure> implements MiddleMeasureService {
    @Override
    public IPage<MiddleMeasure> pageQuery(MachiningReq machiningReq) {
        long end = machiningReq.getEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long start = machiningReq.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        final IPage<DbWheelGroove> page = new Page<>(machiningReq.getPageNo(), machiningReq.getPageSize());
        return baseMapper.pageQuery(page, start, end, machiningReq.getGlassId());
    }

    @Override
    public void export(MachiningReq machiningReq, HttpServletResponse response) {
        machiningReq.setPageNo(-1);
        machiningReq.setPageSize(-1);
        IPage<MiddleMeasure> page = this.pageQuery(machiningReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<MiddleMeasure> middleMeasureList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), MiddleMeasure.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("中间接收测量数据").doWrite(middleMeasureList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public MiddleMeasureGraphResp positionSurveyGraph(MachiningReq machiningReq) {
        long end = machiningReq.getEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long start = machiningReq.getStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        List<MiddleMeasure> middleMeasureList = baseMapper.positionSurveyGraph(start, end);

        if (CollectionUtils.isNotEmpty(middleMeasureList)) {
            MiddleMeasureGraphResp middleMeasureGraphResp = new MiddleMeasureGraphResp();
            //采集时间
            List<String> collectTime = middleMeasureList.stream().map(MiddleMeasure::getDateTime).collect(Collectors.toList());
            middleMeasureGraphResp.setCollectTime(collectTime);
            //左上
            List<String> zs = middleMeasureList.stream().map(MiddleMeasure::getZs).collect(Collectors.toList());
            middleMeasureGraphResp.setZs(zs);
            //左下
            List<String> zx = middleMeasureList.stream().map(MiddleMeasure::getZx).collect(Collectors.toList());
            middleMeasureGraphResp.setZx(zx);
            //右上
            List<String> ys = middleMeasureList.stream().map(MiddleMeasure::getYs).collect(Collectors.toList());
            middleMeasureGraphResp.setYs(ys);
            //右下
            List<String> yx = middleMeasureList.stream().map(MiddleMeasure::getYx).collect(Collectors.toList());
            middleMeasureGraphResp.setYx(yx);
            return middleMeasureGraphResp;
        }
        return null;
    }
}
